package cn.elwy.rcp.core.model.ui;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unchecked")
public class Condition implements Serializable {

  private static final long serialVersionUID = 1L;
  public static final String DATA = "DATA_KEY";

  public static final String AND = " and ";
  public static final String BETWEEN = " between ";
  public static final String EQUAL = "=";
  public static final String GE = " >=";
  public static final String GT = " >";
  public static final String IN = " in ";
  public static final String IS_NOT_NULL = " is not null ";
  public static final String IS_NULL = " is null ";
  public static final String LE = " <=";
  public static final String LIKE = " like ";
  public static final String LT = " <";
  public static final String NE = " <>";
  public static final String NOT_IN = " not in ";
  public static final String OR = " or ";
  public static final String ORDER_BY = " order by ";
  public static final String TRUE = " 1=1 ";

  private Map<String, Object> condition = new HashMap<String, Object>();
  private StringBuffer text = new StringBuffer();

  public Condition() {
  }

  public Condition(Object data) {
    setData(data);
  }

  public void add(String args) {
    text.append(args);
  }

  public void AND() {
    text.append(AND);
  }

  public void BETWEEN(String field, String args1, String args2) {
    text.append(args1).append(BETWEEN).append(args2);
  }

  public void EQUAL(String field, String args) {
    text.append(field).append(EQUAL).append(args);
  }

  public void GE(String field, String args) {
    text.append(field).append(GE).append(args);
  }

  public void GT(String field, String args) {
    text.append(field).append(GT).append(args);
  }

  public void IN(String field, String args) {
    text.append(field).append(IN).append(args);
  }

  public void IS_NOT_NULL(String field) {
    text.append(field).append(IS_NOT_NULL);
  }

  public void IS_NULL(String field) {
    text.append(field).append(IS_NULL);
  }

  public void LE(String field, String args) {
    text.append(field).append(LE).append(args);
  }

  public void LIKE(String field, String args) {
    text.append(field).append(LIKE).append(args);
  }

  public void LT(String field, String args) {
    text.append(field).append(LT).append(args);
  }

  public void NE(String field, String args) {
    text.append(field).append(NE).append(args);
  }

  public void NOT_IN(String field, String args) {
    text.append(field).append(NOT_IN).append(args);
  }

  public void OR() {
    text.append(OR);
  }

  public void ORDER_BY(String field) {
    text.append(ORDER_BY).append(field);
  }

  public void TRUE() {
    text.append(TRUE);
  }

  public <T> T get(String key) {
    return (T) condition.get(key);
  }

  public Map<String, Object> getCondition() {
    return condition;
  }

  public <T> T getData() {
    return (T) condition.get(DATA);
  }

  public void put(String key, Object value) {
    condition.put(key, value);
  }

  public void setData(Object value) {
    condition.put(DATA, value);
  }

  public String getText() {
    return text.toString();
  }

}
